package cn.itcast.embedding;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wys
 * @version V1.0
 * @date 2025-04-20 20:41
 */
@Component
@RequiredArgsConstructor
public class rulerEmbedding {

    @Value("classpath:rulers.txt")
    private Resource resource;

    private final VectorStore vectorStore;

    @PostConstruct
    public void init() throws Exception {
        JSONArray jsonArray = JSONUtil.parseArray(resource.getContentAsString(Charset.defaultCharset()));
        List<Document> documents = new ArrayList<>();
        jsonArray.stream().map(Object::toString).forEach(v -> documents.add(new Document(v)));
        vectorStore.add(documents);
    }
}
